package demo03;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;

public class DruidDemo01 {
    public static void main(String[] args) throws Exception {
        //通过properties集合加载配置文件
        InputStream is = DruidDemo01.class.getClassLoader().getResourceAsStream("druid.properties");
        Properties prop=new Properties();
        prop.load(is);

        //通过druid连接池工厂类获取数据库连接池对象
        DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);

        //获取数据库连接对象
        Connection con = dataSource.getConnection();

        //查询学生信息
        String sql="SELECT * FROM student";
        PreparedStatement pst = con.prepareStatement(sql);
        ResultSet rs = pst.executeQuery();
        while (rs.next()){
            System.out.println(rs.getInt("sid") + "\t" + rs.getString("name") + "\t"
                    + rs.getInt("age") + "\t" + rs.getDate("birthday"));
        }

        //释放资源
        con.close();
        pst.close();
        rs.close();

    }
}
